package com.example.rg1contactapp;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    private EditText editTextName;
    private EditText editTextPhone;

    private Button buttonAdd;
    private Button buttonUpdate;
    private Button buttonDel;
    private Button buttonQuery;
    private Button buttonAll;
    private Button buttonDial;

    private TextView textView;

    private ContactDBHelper contactDBHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editTextName=findViewById(R.id.editTextName);
        editTextPhone=findViewById(R.id.editTextPhone);

        buttonAdd=findViewById(R.id.buttonAdd);
        buttonUpdate=findViewById(R.id.buttonUpdate);
        buttonDel=findViewById(R.id.buttonDel);
        buttonQuery=findViewById(R.id.buttonQuery);
        buttonAll=findViewById(R.id.buttonAll);
        buttonDial=findViewById(R.id.buttonDial);

        textView=findViewById(R.id.textView);

        contactDBHelper=new ContactDBHelper(this);

        buttonAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                long count=  contactDBHelper.insert(editTextName.getText().toString(),editTextPhone.getText().toString());
                if (count>0) Toast.makeText(MainActivity.this,"联系人信息添加成功！",Toast.LENGTH_SHORT).show();
                else Toast.makeText(MainActivity.this,"联系人信息添加失败！",Toast.LENGTH_SHORT).show();
            }
        });

        buttonQuery.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
              String phone=  contactDBHelper.queryByName(editTextName.getText().toString());
              editTextPhone.setText(phone);
              textView.setText(phone);
            }
        });

        buttonAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ArrayList<String> contacts=contactDBHelper.getAll();
                Intent intent=new Intent(MainActivity.this,ContactListActivity.class);
                intent.putExtra("contacts",contacts);
                startActivity(intent);
            }
        });

        buttonDial.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String phone=editTextPhone.getText().toString();
                Intent intent=new Intent();
                intent.setAction("mike");
                intent.setData(Uri.parse("tel:"+phone));
                startActivity(intent);
            }
        });
    }
}
